//
//  GRSlideView.m
//  SleepingElf
//
//  Created by karong on 2023/6/13.
//

#import "GRSlideView.h"

@interface GRSlideView () <UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat paddingLeftRight; // 左右間距
@property (nonatomic, assign) CGFloat paddingTopBottom; // 上下間距
@property (nonatomic, assign) CGFloat cellWidth; // 每一頁的宽度
@property (nonatomic, assign) CGFloat padding; // 間距
@property (nonatomic, strong) NSMutableArray *imagesArr;

@end

const static NSInteger tagView = 1000000;
@implementation GRSlideView

+ (instancetype)slideViewWithFrame:(CGRect)frame padding_left_right:(CGFloat)lr padding_top_bottom:(CGFloat)tb cellWidth:(CGFloat)width {
    GRSlideView *slideView = [[self alloc] initWithFrame:frame];
    slideView.paddingLeftRight = lr;
    slideView.paddingTopBottom = tb;
    slideView.cellWidth = width;
    slideView.padding = 10;
    [slideView setupViewWithFrame:frame];
    return slideView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.paddingLeftRight = 0;
    self.paddingTopBottom = 0;
    self.padding = 0.0f;
    self.isSlide = YES;
    self.cellWidth = CGRectGetWidth(self.frame) * 0.5f;
    [self setupViewWithFrame:self.frame];
}

- (void)setupViewWithFrame:(CGRect)frame {
    CGFloat lWidth = (frame.size.width - self.cellWidth) / 2.0; // 左右宽度
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(lWidth, 0, self.cellWidth, frame.size.height)];
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.layer.masksToBounds = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
}

- (void)setDataArray:(NSArray *)array {
    if (array == nil || array.count == 0) {
        return ;
    }
    self.imagesArr = [NSMutableArray array];
    if (array.count == 1) {
        self.imagesArr = @[array[0],array[0],array[0],array[0],array[0]].mutableCopy;
    } else {
        [self.imagesArr addObject:array[array.count - 2]];
        [self.imagesArr addObject:array[array.count - 1]];
        [self.imagesArr addObjectsFromArray:array];
        [self.imagesArr addObject:array[0]];
        [self.imagesArr addObject:array[1]];
    }
    // 是否輪播
    if (self.isSlide == NO) {
        self.imagesArr = array.mutableCopy;
        CGFloat lWidth = (self.frame.size.width - self.cellWidth) / 2.0; // 左右宽度
        self.scrollView.contentOffset = CGPointMake(lWidth + 10, 0);
        self.scrollView.contentInset = UIEdgeInsetsMake(0, -lWidth, 0, -lWidth);
    } else {
        self.scrollView.delegate = self;
    }
    self.scrollView.contentSize = CGSizeMake(self.cellWidth * self.imagesArr.count, self.frame.size.height);
    for (int i = 0; i < self.imagesArr.count; i++) {
        CGFloat viewWidth = self.cellWidth - self.padding;
        UIView * view = [self.delegate slideView:self viewForItemWithObject:self.imagesArr[i]];
        view.frame = CGRectMake(self.cellWidth * i + self.paddingLeftRight / 2.0, self.paddingTopBottom, viewWidth, self.scrollView.frame.size.height - self.paddingTopBottom * 2);
        [self.scrollView addSubview:view];
        view.tag = tagView + i;
        
        view.userInteractionEnabled = YES;
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapAction:)]];
    }
    
    if (self.isSlide) {
        self.scrollView.contentOffset = CGPointMake(self.cellWidth * 2, 0);
    }
}

- (void)autoSlide {
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.cellWidth, 0);
    } completion:^(BOOL finished) {
        if (self.scrollView.contentOffset.x == self.cellWidth * (self.imagesArr.count - 2)) {
            self.scrollView.contentOffset = CGPointMake(self.cellWidth * 2, 0);
        }
    }];
}

- (void)viewTapAction:(UITapGestureRecognizer *)tap {
    if ([self.delegate respondsToSelector:@selector(slideView:didSelectWithObject:)]) {
        NSInteger clickItem = tap.view.tag - tagView;
        [self.delegate slideView:self didSelectWithObject:self.imagesArr[clickItem]];
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([self.layer containsPoint:point]) {
        NSInteger lWidth = (self.frame.size.width - self.cellWidth) / 2.0; // 左右宽度
        NSInteger x = (point.x + self.scrollView.contentOffset.x - lWidth)/ self.cellWidth;
        UIView *view = [self.scrollView viewWithTag:x + tagView];
        return view;
    }
    return [super hitTest:point withEvent:event];
}

#pragma mark --- scrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger dataCount = self.imagesArr.count;
    if (scrollView.contentOffset.x == 240 * (dataCount - 2)) {
        scrollView.contentOffset = CGPointMake(240 * 2, 0);
    }
    if (scrollView.contentOffset.x == 240) {
        scrollView.contentOffset = CGPointMake(240 * (dataCount - 3), 0);
    }
    NSInteger index = scrollView.contentOffset.x / (self.cellWidth - self.padding);
    if ([self.delegate respondsToSelector:@selector(slideView:didEndDeceleratingWithObject:)]) {
        [self.delegate slideView:self didEndDeceleratingWithObject:self.imagesArr[index]];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger dataCount = self.imagesArr.count;
    CGFloat scrWidth = scrollView.bounds.size.width;
    if (scrollView.contentOffset.x > scrWidth * (dataCount - 2)) {
        CGFloat overflowX = scrWidth * (dataCount - 2) - scrollView.contentOffset.x;
        scrollView.contentOffset = CGPointMake(scrWidth * 2 + overflowX, 0);
    }
    if (scrollView.contentOffset.x < scrWidth) {
        CGFloat overflowX = (scrWidth - scrollView.contentOffset.x);
        scrollView.contentOffset = CGPointMake(scrWidth * (dataCount - 3) - overflowX, 0);
    }
}
@end
